/**
 * window 1.0
 *
 * Copyright (c) 2009 www.01mvc.com
 *
 * http://www.01mvc.com
 *
 * Depends:
 *	ui.core.js
 *	ui.draggable.js
 *	ui.resizable.js
 *  ui.dialog.js
 */
$.fn.window = function(options){

    var defaults = {
        defaultMsg: '<span class="dialogLoading">&nbsp;</span>正在处理,请稍候...',
        sendUrl: "",
		goUrl : "",
		style : "padding:15px; line-height:20px; font-size:13px; text-align:center;",
        dataClass : "posted",
        type : "post",
		dataType : "json",
        stayTime : 2,
		buttons : '',
		height : 'auto',
		width : 300,
		maxHeight : '',
		maxWidth : '',
		minHeight : '',
		minWidth : '',
		title : "提示信息",
		beforeclose : '',
		resizable : true,
		open : '',
		focus : '',
		position : 'center',
		dragStart : '',
		drag : '',
		dragStop : '',
		resizeStart : '',
		resize : '',
		resizeStop : '',
		close : function() {if($(":submit").length > 0)$(":submit").get(0).disabled = false;},
		msgClass : 'dialog-message'+$(".ui-dialog").length

	};
  
    var options = $.extend(defaults, options);
	var msgClass = options.msgClass;
	if(options.type == "post" && options.sendUrl != '')
	{	
		$(this).submit(function(){				

			$(":submit").get(0).disabled = true;
			
			$('<div class="'+msgClass+'" style="'+options.style+'">'+options.defaultMsg+'</div>').dialog({
				buttons : options.buttons,
				height : options.height,
				width : options.width,
				maxHeight : options.maxHeight,
				maxWidth : options.maxWidth,
				minHeight : options.minHeight,
				minWidth : options.minWidth,
				title : options.title,
				beforeclose : options.beforeclose,
				resizable : options.resizable,
				open : options.open,
				focus : options.focus,
				position : options.position,
				dragStart : options.dragStart,
				drag : options.drag,
				dragStop : options.dragStop,
				resizeStart : options.resizeStart,
				buttons: options.buttons,
				close : options.close
			});
	
			$("."+options.dataClass).each(function(){
								   
				if(options.data == '')
				{
					and = '';
				}
				else
				{
					and = '&';
				}
				
				var value = '';
				
				if(this.type == 'checkbox' || this.type == 'radio')
				{
					if(this.checked)
					{
						options.data += and + this.name + '=' + this.value;
					}
				}
				else
				{
					options.data += and + this.name + '=' + this.value;
				}
			});		
			
			$.ajax( {
				type : options.type,
				dataType : options.dataType,
				data : options.data,
				url : options.sendUrl,
				success : function(response) 
				{
					$("."+msgClass).html(response.message);
				  
					if (response.success && options.goUrl != '')
					{
						window.setTimeout(function(){ window.location.href = options.goUrl;} ,options.stayTime * 1000);
					}
					else
					{
						window.setTimeout(function(){$(".ui-dialog:has(."+msgClass+")").hide('slow');$(":submit").get(0).disabled = false;},options.stayTime*1000);
					}
					
					if (response.js != '')
					{
						eval(response.js);
					}
				}
			});
			
			return false;
		});
	}
	else
	{
		$('<div class="'+msgClass+'" style="padding:15px; font-size:13px">'+options.defaultMsg+'</div>').dialog({
			buttons : options.buttons,
			height : options.height,
			width : options.width,
			maxHeight : options.maxHeight,
			maxWidth : options.maxWidth,
			minHeight : options.minHeight,
			minWidth : options.minWidth,
			title : options.title,
			beforeclose : options.beforeclose,
			resizable : options.resizable,
			open : options.open,
			focus : options.focus,
			position : options.position,
			dragStart : options.dragStart,
			drag : options.drag,
			dragStop : options.dragStop,
			resizeStart : options.resizeStart,
			buttons: options.buttons,
			close : options.close
		});
			
		if(options.sendUrl != '')
		{
			$.ajax( {
				type : 'get',
				dataType : options.dataType,
				data : options.data,
				url : options.sendUrl,
				success : function(response) 
				{
					$("."+msgClass).html(response.message);
				  
					if (response.success && options.goUrl != '')
					{
						window.setTimeout(function(){ window.location.href = options.goUrl;} ,options.stayTime * 1000);
					}
					else if(options.stayTime > 0)
					{
						window.setTimeout(function(){$(".ui-dialog:has(."+msgClass+")").hide('slow');},options.stayTime*1000);
					}
					
					if (response.js != '')
					{
						eval(response.js);
					}
				}
			});
		}
		return false;
	}
};